//
//  UserInputViewController.m
//  random-name-generator
//
//  Created by eewert on 10/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UserInputViewController.h"

@implementation UserInputViewController

@synthesize firstNameField;
@synthesize middleNameField;
@synthesize ageField;
@synthesize genderControl;
@synthesize continueButton;
@synthesize clearButton;
@synthesize lexicon;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self resetFields];
    
    lexicon = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Names" ofType:@"plist"]];
    [lexicon retain];
    
    // debugging
    //firstNameField.text = [self getNameForLanguage:@"Eldritch"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [firstNameField resignFirstResponder];
    [middleNameField resignFirstResponder];
    [ageField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [firstNameField resignFirstResponder];
    [middleNameField resignFirstResponder];
    [ageField resignFirstResponder];
}

-(IBAction)continueButtonPress {
    
    // check to see they input anything
    if([firstNameField.text length] >= 1 &&
       [middleNameField.text length] >= 1 &&
       [ageField.text length] >= 1)
    {
        NameTypeSelectionViewController *ntsVC = [[[NameTypeSelectionViewController alloc] initWithNibName:@"NameTypeSelectionViewController" bundle:nil] autorelease];
        [self presentModalViewController:ntsVC animated:YES];
    }
}

-(IBAction)clearButtonPress {
    [self resetFields];
}

// fetches the appropriate language from the lexicon
-(NSArray *)getListsForLanguage:(NSString *)language {
    if(! lexicon) { return NULL; }
    return (NSArray *)[lexicon objectForKey:language];
}

// fetches a strictly gendered name from the lexicon
-(NSString *)getNameForLanguage:(NSString *)language withGender:(NSInteger)gender {
    NSArray *lists = [self getListsForLanguage:language];
    NSArray *genderedNames = (NSArray *)[lists objectAtIndex:gender];
    if ([genderedNames count] < 1) {
        return @"";
    }
    return [genderedNames objectAtIndex:arc4random()%[genderedNames count]];
}

-(NSString *)getNameForLanguage:(NSString *)language withLooseGender:(NSInteger)gender {
    NSString *gendered = [self getNameForLanguage:language withGender:gender];
    if(gender != 0 && [gendered length] < 1 || arc4random() % 2)
    {
        NSString *neut = [self getNameForLanguage:language withGender:0];
        return neut;
    }
    return gendered;
}

-(NSString *)getNameForLanguage:(NSString *)language {
    NSInteger gender = [genderControl selectedSegmentIndex];
    NSString *first = [self getNameForLanguage:language withLooseGender:gender];
    NSString *last = [self getNameForLanguage:language withLooseGender:gender];
    return [NSString stringWithFormat:@"%@ %@", first, last];
}

-(void)resetFields {
    // clear all input fields
    [firstNameField setText:firstNameField.placeholder];
    [middleNameField setText:middleNameField.placeholder];
    [ageField setText:ageField.placeholder];
    genderControl.selectedSegmentIndex = 0;
}

@end
